package SubServer;

import General.Product;
import General.ShipQuoterServiceInterface;

public class ShipmentSimulation extends Thread {
	
	private ShipQuoterService fQuoter;
	private Shipment fShipment;
	
	public ShipmentSimulation(ShipQuoterService quoter, Shipment shipment) {
		fQuoter = quoter;
		fShipment = shipment;
	}
	
	public void run() {
		try {
			// Output
			System.out.println("[SUBSERVER] Shipment started for product with id " + fShipment.getProduct().getId());
			
			// Make product ready for shipment after 5 seconds
			Thread.sleep(5000);
			fShipment.setState(Product.READY);
			fShipment.getClientEvent().notification(fShipment.getProduct(), Product.READY);
			System.out.println("[SUBSERVER] State changed to Ready for product with id " + fShipment.getProduct().getId());
			
			// Sent product after 5 seconds
			Thread.sleep(5000);
			fShipment.setState(Product.SENT);
			fShipment.getClientEvent().notification(fShipment.getProduct(), Product.SENT);
			System.out.println("[SUBSERVER] State changed to Sent for product with id " + fShipment.getProduct().getId());
			
			// Product arrives after 5 seconds
			Thread.sleep(5000);
			fShipment.setState(Product.ARRIVED);
			fShipment.getClientEvent().notification(fShipment.getProduct(), Product.ARRIVED);
			System.out.println("[SUBSERVER] State changed to Arrived for product with id " + fShipment.getProduct().getId()); 
			
			// Shipment finished after 5 seconds
			Thread.sleep(5000);
			fShipment.setState(Product.FINISHED);
			fQuoter.changeToFinished(fShipment);
			fShipment.getClientEvent().notification(fShipment.getProduct(), Product.FINISHED);
			System.out.println("[SUBSERVER] Shipment finished for product with id " + fShipment.getProduct().getId());
					
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
